package cn.edu.scau;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class DownloadImages {

	private static final String webSiteURL = "http://www.baidu.com";
	private static final String folderPath = "download";
	private static ArrayList<String> list = new ArrayList<>();

	public static void main(String[] args) {
		getFileList();
		// Proxy();
		startFetch();
		System.out.println("Finish Fetching!");
	}

	private static void getFileList() {
		File files = new File(folderPath);
		for (File file : files.listFiles()) {
			String fileName = file.getName();
			list.add(fileName);
		}
		// for (String string : list) {
		// System.out.println(string);
		// }
	}

	private static void startFetch() {
		try {
			Document doc = Jsoup.connect(webSiteURL).get();
			Elements a = doc.getElementsByTag("a");
			for (Element el : a) {
				String link = el.absUrl("href");
				System.out.println(link);
				downloadImages(link);
			}
		} catch (IOException ex) {
			System.err.println("There was an error");
			Logger.getLogger(DownloadImages.class.getName()).log(Level.SEVERE,
					null, ex);
		}
	}

	private static void Proxy() {
		System.getProperties().setProperty("proxySet", "true");
		// 用的代理服务器
		System.getProperties().setProperty("http.proxyHost", "127.0.0.1");
		// 代理端口
		System.getProperties().setProperty("http.proxyPort", "8087");
	}

	private static void downloadImages(String webSiteURL) {
		try {
			Document doc = Jsoup.connect(webSiteURL).get();
			Elements img = doc.getElementsByTag("img");
			for (Element el : img) {
				String src = el.absUrl("src");
				System.out.println("Image Found!");
				System.out.println("src attribute is : " + src);
				getImages(src);
			}
		} catch (IOException ex) {
			System.err.println("There was an error");
			Logger.getLogger(DownloadImages.class.getName()).log(Level.SEVERE,
					null, ex);
		}
	}

	private static void getImages(String src) throws IOException {
		int indexname = src.lastIndexOf("/");
		if (indexname == src.length()) {
			src = src.substring(1, indexname);
		}
		indexname = src.lastIndexOf("/");
		String name = src.substring(indexname, src.length());
		if (list.contains(name.substring(1))) {
			System.out.println(name + " has existed!");
			return;
		}
		System.out.println(name);
		URL url = new URL(src);
		InputStream in = url.openStream();
		OutputStream out = new BufferedOutputStream(new FileOutputStream(
				folderPath + name));
		for (int b; (b = in.read()) != -1;) {
			out.write(b);
		}
		out.close();
		in.close();
	}
}